﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.loto.Common;
using com.huigo.Framework.ServiceUtils;

namespace com.huigo.Admin.Web.financial
{
    public partial class AddSupplement : BasePage
    {
        private bool isModify
        {
            get
            {
                return sid == long.MinValue ? flgModify.Text == "0" ? false : true : true;
            }
        }

        private long sid
        {
            get
            {
                if (string.IsNullOrEmpty(Request.QueryString["sid"]))
                {
                    return long.MinValue;
                }
                else
                {
                    long s = long.MinValue;
                    if (long.TryParse(Request.QueryString["sid"], out s))
                        flgModify.Text = "1";
                    return s;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!VerifyField(FieldType.STRING, custName.Text, true))
            {
                MessageBox.Show(this.Page, "经销商不能为空！");
                return;
            }

            if (!checkCustName(custName.Text.Trim()))
            {
                MessageBox.Show(this.Page, "该经销商不存在，请重新输入");
                return;
            }
            if (!VerifyField(FieldType.STRING,feeType.SelectedValue, true))
            {
                MessageBox.Show(this.Page, "请选择充值方式！");
                return;
            }
            if (!VerifyField(FieldType.STRING, payPrice.Text, true))
            {
                MessageBox.Show(this.Page, "充值金额不能为空！");
                return;
            }
            if (Convert.ToDecimal(payPrice.Text) <= 0)
            {
                MessageBox.Show(this.Page, "充值金额必须大于0！");
                return;
            }
            if (!VerifyField(FieldType.STRING, payNO.Text, true))
            {
                MessageBox.Show(this.Page, "支付交易号不能为空！");
                return;
            }
            if (!VerifyField(FieldType.STRING, meno.Text, true))
            {
                MessageBox.Show(this.Page, "充值备注不能为空！");
                return;
            }
            List<RequestFuncParams> argList = new List<RequestFuncParams>();
            argList.Add(new RequestFuncParams("idCustFid", custFid.ToString()));
             argList.Add(new RequestFuncParams("userID", UserId.ToString()));
             argList.Add(new RequestFuncParams("payNo", payNO.Text.Trim()));
             argList.Add(new RequestFuncParams("meno", meno.Text.Trim()));
             argList.Add(new RequestFuncParams("PayFee", payPrice.Text.Trim()));
             argList.Add(new RequestFuncParams("channel", feeType.SelectedValue));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.INSERT_PAY_FEE_FLOW, argList);

            if (parser != null && parser.RowsCount > 0 && parser["returnargs", 0] == "true")
            {
                GoTo(GoToType.ADDSUC);
            }
        }
        long custFid = 0;
        private bool checkCustName(string custName)
        { 
          List<RequestFuncParams> argList = new List<RequestFuncParams>();
          argList.Add(new RequestFuncParams("userName", custName));
            BaseService baseService = new BaseService();
            ParserXml parser = baseService.RequestEntryService(Dict.ERequestFunc.CHECK_USER_NAME, argList);

            if (parser != null && parser.RowsCount > 0)
            {
                custFid = Convert.ToInt64(parser["CustId", 0]);
                return true;
            }
            return false;
        }
        private void LoadData()
        {

            //充值方式
            List<DropList> statusDropList = Tools.DictToDropList(Dict.DictStringPayType);
            if (statusDropList != null)
            {
                feeType.DataSource = statusDropList;
                feeType.DataValueField = "Id";
                feeType.DataTextField = "Value";
                feeType.DataBind();
            };
        }
    }
}
